<?php
// +----------------------------------------------------------------------
// | astp [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Author 荒年 < 931274989@qq.com >
// +----------------------------------------------------------------------
// | Explain 客服消息
// +----------------------------------------------------------------------
namespace app\wewhat\controller;

use think\Controller;
use app\wewhat\service\HttpRequest;
use app\wewhat\service\Accesstoken;

class Kefu extends Controller
{
  public function addMsg()
  {
    if(request()->isPost())
    {
      $post = $this->request->post();
      $openID = $post['openID'];
      $msg = $post['msg'];
      $this->kefuMsg($openID,$msg);
      $this->success('发送成功');
    } else {
      return view();
    }
  }

  public function kefuMsg($openID,$msg)
  {
    $access = new Accesstoken();
    $http = new HttpRequest();
    $url = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token='.$access->getAccessToken();
    $data = '{
        "touser":"' .$openID. '",
        "msgtype":"text",
        "text":
        {
          "content":"' .$msg. '"
        }
    }';
    $json = $http->http_request($url,$data);
    return $json;
  }

}

?>